package com.example.controller;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

import com.example.listenanddriveproject.R;
import com.example.settings.CercleSound;
import com.example.settings.Dilate;
import com.example.settings.Erode;
import com.example.settings.Homography;
import com.example.settings.Hsv;
import com.example.settings.Setting;
import com.example.settings.VideoSettings;
import com.example.storage.Storage;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener{
	
	private ActionBar mBar;
	private ViewPager mPager;
	private PagerAdapter mAdapter;
	public static Setting settings = new Setting();
	
	private String[] tabs = { "Video", "Homographie", "HSV", "Morphologie", "Options video", "Autres" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		if(!Storage.fileExists(getApplication())){
			MainActivity.settings.setErodeSettings(new Erode(1,true));
			MainActivity.settings.setDilateSettings(new Dilate(3,true));
			MainActivity.settings.setCercleSound(new CercleSound(10,4));
			MainActivity.settings.setHomography(new Homography(7715,26731));
			MainActivity.settings.setVideoSettings(new VideoSettings(true, false, false, false, true));
			MainActivity.settings.setHsv(new Hsv(0,8,175,179));
			MainActivity.settings.saveArray(this.getApplication());
		}else{
			MainActivity.settings.setArray(Storage.load(getApplication()));
		}
		
		mPager = (ViewPager) findViewById(R.id.pager);
		mBar = getActionBar();
		mAdapter = new PagerAdapter(getSupportFragmentManager());

		mPager.setAdapter(mAdapter);
		mBar.setHomeButtonEnabled(false);
		mBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);		
		
		for (String tab_name : tabs) {
			mBar.addTab(mBar.newTab().setText(tab_name)
					.setTabListener(this));
		}
		
		mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// on changing the page
				// make respected tab selected
				mBar.setSelectedNavigationItem(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});


	}
		
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		mPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
    public void onPause()
    {
        super.onPause();
        finish();
    }

}
